/*
 * Copyright [duowan.com]
 * Web Site: http://www.duowan.com
 * Since 2005 - 2014
 */

package com.duowan.realtime.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.duowan.common.util.page.Page;
import com.duowan.realtime.dao.ServerInfoDao;
import com.duowan.realtime.model.ServerInfo;
import com.duowan.realtime.query.ServerInfoQuery;
import com.duowan.realtime.service.ServerInfoService;


/**
 * [ServerInfo] 的业务操作实现类
 * 
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */
@Service("serverInfoService")
@Transactional
public class ServerInfoServiceImpl implements ServerInfoService {

	protected static final Logger log = LoggerFactory.getLogger(ServerInfoServiceImpl.class);
	
	//
	// 请删除无用的方法，代码生成器只是为你生成一个架子
	//
	
	private ServerInfoDao serverInfoDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setServerInfoDao(ServerInfoDao dao) {
		this.serverInfoDao = dao;
	}
	
	/** 
	 * 创建ServerInfo
	 **/
	public ServerInfo create(ServerInfo serverInfo) {
	    Assert.notNull(serverInfo,"'serverInfo' must be not null");
	    initDefaultValuesForCreate(serverInfo);
	    new ServerInfoChecker().checkCreateServerInfo(serverInfo);
	    serverInfoDao.insert(serverInfo);
	    return serverInfo;
	}
	
	/** 
	 * 更新ServerInfo
	 **/	
    public ServerInfo update(ServerInfo serverInfo) {
        Assert.notNull(serverInfo,"'serverInfo' must be not null");
        new ServerInfoChecker().checkUpdateServerInfo(serverInfo);
        serverInfoDao.update(serverInfo);
        return serverInfo;
    }	
    
	/** 
	 * 删除ServerInfo
	 **/
    public void removeById(int id) {
        serverInfoDao.deleteById(id);
    }
    
	/** 
	 * 根据ID得到ServerInfo
	 **/    
    public ServerInfo getById(int id) {
        return serverInfoDao.getById(id);
    }
    
	/** 
	 * 分页查询: ServerInfo
	 **/      
	@Transactional(readOnly=true)
	public Page<ServerInfo> findPage(ServerInfoQuery query) {
	    Assert.notNull(query,"'query' must be not null");
		return serverInfoDao.findPage(query);
	}
	
    
	/**
	 * 为创建时初始化相关默认值 
	 **/
    public void initDefaultValuesForCreate(ServerInfo serverInfo) {
    }
    
    /**
     * ServerInfo的属性检查类,根据自己需要编写自定义检查
     **/
    public class ServerInfoChecker {
        /**可以在此检查只有更新才需要的特殊检查 */
        public void checkUpdateServerInfo(ServerInfo serverInfo) {
            checkServerInfo(serverInfo);
        }
    
        /**可以在此检查只有创建才需要的特殊检查 */
        public void checkCreateServerInfo(ServerInfo serverInfo) {
            checkServerInfo(serverInfo);
        }
        
        /** 检查到有错误请直接抛异常，不要使用 return errorCode的方式 */
        public void checkServerInfo(ServerInfo serverInfo) {
        	// Bean Validator检查,属性检查失败将抛异常
            //validateWithException(serverInfo);
            
        	//复杂的属性的检查一般需要分开写几个方法，如 checkProperty1(v),checkProperty2(v)
        }
    }

	@Override
	public List<ServerInfo> getAll() {
		return serverInfoDao.getAll();
	}

	@Override
	public ServerInfo getServerIp(String realtimeComputingGroup) {
		ServerInfo serverInfo = serverInfoDao.getServerIps(realtimeComputingGroup);
		if (serverInfo == null) {
			throw new RuntimeException("Cannot get server ip by realtimeComputingGroup");
		}
		return serverInfo;
	}
}
